<?php

namespace Hilaoyu\LaravelExtend\Services\Auth;

use Carbon\Carbon;
use Hilaoyu\LaravelExtend\ControllerBaseResource;
use Hilaoyu\Utils\UtilHtml;
use Hilaoyu\Utils\UtilSupport;
use Illuminate\Http\Request;


abstract class AbstractControllerAuthUsers extends ControllerBaseResource
{


    protected function alterModelsQuery($query,Request $request)
    {
        $query = parent::alterModelsQuery($query,$request);
        $search = [
            'keyword' => $request->input('keyword',''),
            'status' => $request->input('status',''),
            'time_start' => $request->input('time_start',''),
            'time_end' => $request->input('time_end',''),
        ];



        if($search['time_start'] != ''){
            $query = $query->where('created_at','>=',$search['time_start']);
        }

        if($search['time_end'] != ''){
            $query = $query->where('created_at','<=',$search['time_end']);
        }

        if($search['status'] != ''){
            if('1' == $search['status']){
                $query = $query->where(function ($q){
                    return $q->orWhere('lock_to','<=',Carbon::now()->toDateTimeString())
                        ->orWhereNull('lock_to')
                        ;
                });
            }else{
                $query = $query->where('lock_to','>',Carbon::now()->toDateTimeString());
            }

        }



        if(!empty($search['keyword'])){
            $query = $query->where(function ($query) use ($search){
                $query->orWhere('account','like','%'. $search['keyword'] .'%')
                    ->orWhere('name','like','%'. $search['keyword'] .'%')
//                    ->orWhere('summary','like','%'. $search['keyword'] .'%')
                    ->orWhere('email','like','%'. $search['keyword'] .'%')
                    ->orWhere('mobile','like','%'. $search['keyword'] .'%');
            });
        }


        $formSearch = [
            'keyword' => [
                'value' => $search['keyword'],
                'label' => '账号/昵称/邮箱/手机',
                'type' => 'text',
            ] ,
            'status' => [
                'value' => $search['status'],
                'label' => '状态',
                'type' => 'select',
                'options'=> UtilHtml::statusOptions($search['status'],'全部状态','启用','禁用')
            ] ,
            'time_start' => [
                'value' => $search['time_start'],
                'label' => '注册时间起始',
                'type' => 'dateTime',
            ] ,
            'time_end' => [
                'value' => $search['time_end'],
                'label' => '注册时间截止',
                'type' => 'dateTime',
            ] 
        ];

        comResponse()->with( $formSearch,'formSearch');
        return $query;
    }

    protected function alterModelsLoaded($models, Request $request)
    {
        $models = parent::alterModelsLoaded($models, $request);

        return $models;
    }


    protected function alterModelLoaded($model, Request $request)
    {
        $model = parent::alterModelLoaded($model, $request);

        comResponse()
            //->with( $role_options,'role_options')
            ->with( UtilHtml::sexOptions(($model->sex ?? '保密')),'sex_options')
            ->with( UtilHtml::statusOptions(($model->is_super_administrator ?? '0')),'is_super_options');
        return $model;
    }


    protected function alterModelSaveData(Request $request,$id='')
    {
        $data = [
            //'account' => $request->input('account',''),
            'name' => $request->input('name',''),
            'summary' => $request->input('summary',''),
            'sex' => $request->input('sex',''),
            'email' => $request->input('email',''),
            'mobile' => $request->input('mobile',''),
            'picture' => $request->input('picture',''),
            'lock_to' => $request->input('lock_to',null),
            'roles' => $request->input('roles',[]),
        ];

        if(empty($id)){
            $data['account'] = $request->input('account','');
        }

        if($pass = $request->input('password',null)){
            $data['password'] = $pass;
        }

        return $data;
    }



}
